/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.sonar.services;

import java.util.Set;
import org.internna.iwebjtracker.sonar.model.SonarMetrics;
import org.internna.iwebjtracker.sonar.model.SonarConfiguration;

/**
 * Handles Sonar access and manages the data retrieved.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public interface SonarManager {

    /**
     * Subset of allowed metrics in IWebJTracker from those available in Sonar.
     */
    enum METRICS {

        UNIT_TESTS("tests"), UNIT_TEST_FAILURES("test_failures"), TEST_COVERAGE("coverage"), RULES_VIOLATIONS("violations"), BLOCKER_VIOLATIONS("blocker_violations"), CRITICAL_VIOLATIONS("critical_violations"), LINES_OF_CODE("lines"), LINES_OF_CODE_WITHOUT_COMMENTS("ncloc"), DUPLICATED_LINES("duplicated_lines"), DUPLICATED_LINES_PERCENTAGE("duplicated_lines_density"), COMPLEXITY("complexity"), DOCUMENTED_API("public_documented_api_density"), TECHNICAL_DEBT("technical_debt");

        private String metric;

        private METRICS(String metric) {
            this.metric = metric;
        }

        /**
         * The metric as Sonar requires it in the URL.
         *
         * @return a non null non empty String.
         */
        @Override public String toString() {
            return metric;
        }

        /**
         * Gets the enum from the stored metric value
         *
         * @param metric any
         * @return any
         */
        public static METRICS fromMetric(String metric) {
            for (METRICS value : METRICS.values()) {
                if (value.toString().equals(metric)) {
                    return value;
                }
            }
            return null;
        }

    }

    /**
     * Access Sonar Web Service API.
     *
     * http://nemo.sonarsource.org/api/resources?resource=org.apache.maven:maven&depth=0&metrics=ncloc,duplicated_lines_density,violations,blocker_violations,critical_violations,tests,test_failures,coverage,complexity,public_documented_api_density,technical_debt
     * 
     * @param configuration any (if null no operation performed)
     * @param metrics any (if null no metrics are returned but project information only)
     * @return any
     */
    SonarMetrics getMetricData(SonarConfiguration configuration, Set<METRICS> metrics);

}
